<?php 

class PublicModule
{
	
	public $openedObject, $urlParts = array(), $objects = array(), $title = array(), $languages = false, $metaDescription;
	
	public $template, $language;
	
	public $html, $css = array(), $js = array();
	
	public function __construct()
	{
		$this->GetTemplate();
		$this->title = array_reverse($this->title);
	}
	
	public function GetTemplate()
	{
		$params = array('active' => 1, 'limit' => 1, 'parent'=>0);
		$obj = new Object;
		
		if(empty($_GET['rewrite'])) //get first object
		{
			$list = $obj->getList($params);
			if(sizeof($list))
			{
				$this->openedObject = $list[0];
				$this->title[] = $this->openedObject->name; 
				$this->objects[] = $list[0];
				$this->metaDescription = $this->openedObject->alt_description;
			}
			else
			{
				$this->template = '404';
			}
		}
		else
		{
			$this->urlParts =  explode('/', $_GET['rewrite']);
			//$url = $this->urlParts;
			$pid = 0;
			foreach($this->urlParts as $key=>$rewrite)
			{
				if($rewrite == '' || is_null($rewrite))
				{
					unset($this->urlParts[$key]);
					continue;
				}
				
				$object = $obj->getByRewrite($rewrite, $pid);
				if($object)
				{
					$pid = $object->id;
					$this->objects[] = $object;
					unset($this->urlParts[$key]);
					$this->openedObject = $object;
					if($object->template == 'language')
					{
						$this->language = strtolower($this->name);
					}
					else
					{
						$this->title[] = $object->name;
					}
					
					if(!empty($object->alt_description))
					{
						$this->metaDescription = $object->alt_description;
					}
				}
				
			}
			
			if(sizeof($this->urlParts) && !$this->openedObject)
			{
				$this->template = '404';
			}
		}
		
		if($this->openedObject)
		{
			$this->openedObject->loadValues();
		}
		
		$this->getLanguages();
	
	}
	
	
	public function getTitle()
	{
		if($this->openedObject && !empty($this->openedObject->alt_title))
		{
			$this->title = array();
			$this->title[] = $this->openedObject->alt_title;
		}
		return $this->title;
	}

	public function content()
	{
		global $smarty;
		
		$smarty->template_dir[] = PATH . 'project/templates';
		
		$path = PATH . 'project/templates/';
		
		if($this->openedObject && $this->template!='404')
		{
			$this->getTranslations();
			
			$smarty->assign('languages', $this->languages);
			$smarty->assign('menu', $this->getMenu());
			$smarty->assign('opened', $this->openedObject);
			$smarty->assign($this->openedObject->fetchData());
		
		if(file_exists($path . $this->openedObject->template . '.php'))
		{
			include_once($path . $this->openedObject->template . '.php');
			$module = new Module($this);
			$smarty->assign($module->assign);
		}
			
		if(file_exists($path . $this->openedObject->template . '.css'))
		{
			$this->_add_css( BASE . 'project/templates/' . $this->openedObject->template . '.css');
		}
			
			if(file_exists($path . $this->openedObject->template . '.js'))
			{
				$this->_add_js( BASE . 'project/templates/' . $this->openedObject->template . '.js');
			}
		
		}
		if($this->template == '404' || ( sizeof($this->urlParts) && $this->openedObject) )
		{
			unset($this->js, $this->css);
			$this->title[] = 'Error 404 - Page not found';
			header("HTTP/1.0 404 Not Found");
			$template = '404.tpl';
		}
		else
		{
			$template =  $this->openedObject->template . '.tpl';
		}
		
		$this->html = $smarty->fetch($path . $template);
		
	}
	
	public function getLanguages()
	{
		$ids = $this->getOpenedIds();
		
		$obj = new Object;
		$params = array('parent' => 0, 'template' => 'language');
		$list = $obj->getList($params);
		
		if(!$list)
		{
			return false;
		}
		
		
		$this->languages = $list;
		
	}
	
	
	public function getMenu($root=0)
	{
		//calculate root id
		if($root==0 && $this->languages)
		{
			foreach($this->languages as $lang)
			{
				if($lang->opened)
				{
					$root = $lang->id;
				}
			}			
		}
		
		$obj = new Object;
		$params = array('parent' => $root, 'active' => 1, 'showonmenu'=>1);
		
		$list = $obj->getList($params);
		
		foreach($list as $key=>$item)
		{
			if(in_array($item->id, $this->getOpenedIds()))
			{
				$list[$key]->opened = true;
				$list[$key]->children = $this->getMenu($item->id);
			}
		}
		
		return $list;
		
	}
	
	
	public function getOpenedIds()
	{
		$ids = array();
		foreach($this->objects as $obj)
		{
			$ids[] = $obj->id;
		}
		
		return $ids;
	}
	
	
	public function _add_css($file)
	{
		$this->css[] = $file;
	}
	
	public function _add_js($file)
	{
		$this->js[] = $file;
	}
	
	public function getTranslations()
	{
		if(empty($this->language))
		{
			$config = _siteConfig::getConfig();
			if(isset($config['languages'][0]))
			{
				$this->language = $config['languages'][0];
			}
		}
		
		if(empty($this->language))
		{
			return array();
		}
		
		$tra = _translation::getTranslations(array('loadgroup'=>true));
		
		global $TRA;
		$TRA = array();
		
		foreach($tra as $item)
		{
			$TRA[$item['groupName']][$item['ident']] = $item['values'][$this->language];
		}
		
		return true;
	}
	
	
	
	
	
	
}


?>